package net.sudot.chess.business.dao;

import net.sudot.chess.business.model.CustomerRank;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * 客户等级
 *
 * @author tangjialin on 2018-03-17.
 */
@Repository
public class CustomerRankDao extends BaseDao<CustomerRank> {

    /**
     * 查找默认会员等级
     *
     * @return 默认会员等级，若不存在则返回null
     */
    public CustomerRank findDefault() {
        return find("isDefault", true);
    }

    /**
     * 根据押金金额查找符合此条件的最高会员等级
     *
     * @param deposit 押金金额
     * @return 会员等级, 若不存在则返回null
     */
    public CustomerRank findByDeposit(BigDecimal deposit) {
        String jpql = "SELECT customerRank FROM CustomerRank customerRank WHERE customerRank.deposit <= :deposit ORDER BY customerRank.deposit DESC";
        return uniqueResult(entityManager.createQuery(jpql, CustomerRank.class).setParameter("deposit", deposit));
    }

}
